package dao;

import pojo.Note;
import util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ImplementManager implements AreaManager{
    /**
     * 判断区域管理员登录的用户名和密码是否正确
     * @param type
     * @param managername
     * @param managerpassword
     * @return
     */
    @Override
    public boolean managerLogin(String type,String managername, String managerpassword) {
        boolean flag=false;
        try {
            JDBC jdbc=new JDBC();
            jdbc.dbconn();
            ResultSet rs= JDBC.selectsql("select * from manager where type='"+type+"'");
            while(rs.next()) {
                if (rs.getString("type").equals(type) && rs.getString("managername").equals(managername)) {
                    if (rs.getString("managerpassword").equals(managerpassword)){
                        flag = true;
                        break;
                    }
                }
            }
            jdbc.closeconn();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     *查找在该区域发布过笔记的用户，将用户名封装成集合
     * @return
     */
    public ArrayList<String> checkuser(String type) {
        ArrayList<String> listusername=new ArrayList<>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
            ResultSet rs= JDBC.selectsql("select usename from biji where type='"+type+"'");
            while(rs.next()){
                listusername.add(rs.getString("usename"));
            }
            jdbc.closeconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return listusername;
    }

    /**
     * 查找某个具体用户的所有笔记
     * @param usename
     * @return
     */
    @Override
    public ArrayList<Note> userNote(String usename) {
        ImplementNote implementNote=new ImplementNote();
        ArrayList<Note> allnotes=implementNote.showAllNotes();
        ArrayList<Note> usernotes=new ArrayList<Note>();
        for (Note note : allnotes) {
            if(usename.equals(note.getUsename())){
                usernotes.add(note);
            }
        }
        return usernotes;
    }

    /**
     * 接收请求的用户名和title，查找笔记表里的所有笔记，将对应的笔记删除
     * @param usename
     * @param title
     * @return
     */
    public boolean deleteusernote(String usename,String title){
        boolean flag=false;
        ImplementNote implementNote=new ImplementNote();
        flag=implementNote.deleteNote(usename,title);
        return flag;
    }

    /**
     * 获取某个区域内所有笔记
     * @param type
     * @return
     */
    @Override
    public ArrayList<Note> areaNote(String type) {
        ArrayList<Note> areaNote=new ArrayList<Note>();
        JDBC jdbc=new JDBC();
        try {
            jdbc.dbconn();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        ResultSet rs=jdbc.selectsql("select * from biji where type='"+type+"'");
        while(true){
            try {
                if (!rs.next()) break;
                Note note=new Note();
                note.setStyle(Integer.parseInt(rs.getString("style")));
                note.setId(rs.getString("id"));
                note.setTitle(rs.getString("title"));
                note.setUsename(rs.getString("usename"));
                note.setContent(rs.getString("content"));
                note.setType(rs.getString("type"));
                note.setUsename(rs.getString("usename"));
                areaNote.add(note);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return areaNote;
    }
}
